package Engine.UIControl
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ScrollBase
	{
		private var _content:Sprite;
		private var _mask:Sprite;
		
		private var _currentY:Number = 0;
		private var _stepY:Number = 70; //每次移动的像素
		
		private var _dragbtn:DragGUI;	//滚动条
		private var _upBtn:Button;		//上按钮
		private var _downBtn:Button;	//下按钮
		
		public function ScrollBase(bg:DisplayObjectContainer,upbtn:Sprite,downbtn:Sprite,dragbtn:Sprite)
		{
			this._content = new Sprite();
			bg.addEventListener(MouseEvent.MOUSE_WHEEL,onMouseWheel);
			bg.addChild(this._content);
			
			
			this._mask = new Sprite();
			this._mask.graphics.lineStyle();
			this._mask.graphics.beginFill(0xFFFFFF);
			this._mask.graphics.drawRect(0, 0, bg.width, bg.height);
			this._mask.graphics.endFill();
			bg.addChild(_mask);
			this._content.mask = _mask;
			
			this._dragbtn = new DragGUI(dragbtn);
			this._dragbtn.setMinMaxY(upbtn.y+upbtn.height,downbtn.y);
			this._dragbtn.setUpdateYFun(_updateY);
			
			this._upBtn = new Button(upbtn);
			this._upBtn.setOnClickFun(_up);
			
			this._downBtn = new Button(downbtn);
			this._downBtn.setOnClickFun(_down);
			
			this._currentY = 0;
		}
		
		private function _up():void
		{
			if(this._mask.height >= this._content.height)
				return;
			var posy:Number = this._content.y + this._stepY;
			if(posy >= 0)
				this._content.y = 0;
			else
				this._content.y = posy;
			setMoveBarPos();
		}
		
		private function _down():void
		{
			if(this._mask.height >= this._content.height)
				return;
			var posy:Number = this._content.y - this._stepY;
			if (posy + this._content.height <= this._content.height)
				this._content.y = this._mask.height - this._content.height;
			else
				this._content.y = posy;
			setMoveBarPos();
				
		}
		
		private function setMoveBarPos():void
		{
			this._dragbtn.y = this._dragbtn.dragHeight* (-this._content.y) / (this._content.height-this._mask.height);
		}
		
		private function onMouseWheel(event:MouseEvent):void
		{
			if(event.delta > 0)
				this._up();
			else
				this._down();
		}
		
		private function _updateY(offsetY:Number):void
		{
			var posy:Number = offsetY*(this._content.height - this._mask.height)/this._dragbtn.dragHeight;
			this._content.y = -posy;
		}
		
		public function resetBtn():void
		{
			if(this._mask.height >= this._content.height)
				this._dragbtn.setDrag(false,false);
			else
				this._dragbtn.setDrag(false,true);
		}
		
		public function addChild(_child:DisplayObject):void
		{
			this._content.addChild(_child);
			_child.y = this._currentY;
			this._currentY += _child.height+1;
			this.resetBtn();
		}
		
		public function clearItem():void
		{
			while(this._content.numChildren)
			{
				this._content.removeChildAt(0);
			}
			this._content.y = 0;
			this._currentY = 0;
			this.resetBtn();
		}
		
		public function setDragStepY(p_stepY:int):void
		{
			this._stepY = p_stepY;
		}
		
		public function get content():Sprite
		{
			return this._content;
		}
	}
}